package com.corpgovernment.supplier.controller;

import java.util.List;
import java.util.stream.Collectors;

import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.corpgovernment.api.supplier.bo.suppliercompany.GetSupplierCompanyRequestBo;
import com.corpgovernment.api.supplier.bo.suppliercompany.GetSupplierProductRequestBo;
import com.corpgovernment.api.supplier.bo.suppliercompany.ListPubOwnRequestBo;
import com.corpgovernment.api.supplier.bo.suppliercompany.ListSupplierCompanyRequestBo;
import com.corpgovernment.api.supplier.bo.suppliercompany.ListSupplierProductRequestBo;
import com.corpgovernment.api.supplier.bo.suppliercompany.SupplierCompanyBo;
import com.corpgovernment.api.supplier.bo.suppliercompany.SupplierProductBo;
import com.corpgovernment.api.supplier.bo.suppliercompany.querycompanysupplierproduct.QueryCompanySupplierProductRequest;
import com.corpgovernment.api.supplier.bo.suppliercompany.querycompanysupplierproduct.QueryCompanySupplierProductResponse;
import com.corpgovernment.api.supplier.vo.MbSupplierInfoVo;
import com.corpgovernment.api.supplier.vo.request.ListSupplierUrlReqVo;
import com.corpgovernment.api.supplier.vo.request.LoadSupplierInfoReq;
import com.corpgovernment.api.supplier.vo.response.ListSupplierUrlRespVo;
import com.corpgovernment.common.base.BaseUserInfo;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.common.operatelog.OperateLog;
import com.corpgovernment.common.operatelog.constant.OperateLogContant;
import com.corpgovernment.organization.service.OpenCardService;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.corpgovernment.supplier.dto.SupplierCompanyDto;
import com.corpgovernment.supplier.entity.db.MbSupplierInfo;
import com.corpgovernment.supplier.service.SupplierCompanyService;
import com.corpgovernment.supplier.vo.GetSyncOpenCardResultDetailRequestVo;
import com.corpgovernment.supplier.vo.GetSyncOpenCardResultRequestVo;
import com.corpgovernment.supplier.vo.OpenCardDetailListInfoVo;
import com.corpgovernment.supplier.vo.OpenCardListInfoVo;
import com.corpgovernment.supplier.vo.suppliercompany.AddOrUpdateRequestVo;
import com.corpgovernment.supplier.vo.suppliercompany.DeleteRequestVo;
import com.corpgovernment.supplier.vo.suppliercompany.EnableRequestVo;
import com.corpgovernment.supplier.vo.suppliercompany.InitRequestVo;
import com.corpgovernment.supplier.vo.suppliercompany.InitResponseVo;
import com.corpgovernment.supplier.vo.suppliercompany.ListRequestVo;
import com.corpgovernment.supplier.vo.suppliercompany.SortRequestVo;
import com.corpgovernment.supplier.vo.suppliercompany.SupportSupplierInfoVo;
import com.corpgovernment.supplier.vo.suppliercompany.SyncOpenCardRequestVo;
import com.corpgovernment.supplier.vo.suppliercompany.SyncOpenCardResponseVo;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.JsonUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @author jhxue
 */
@Api(tags = "企业订购")
@RestController
@RequestMapping("/supplierCompany")
@Slf4j
public class SupplierCompanyController {

	@Autowired
	private SupplierCompanyService supplierCompanyService;
    @Autowired
    private OpenCardService openCardService;

	/**
	 * 企业订购列表
	 */
	@RequestMapping("/list")
	public JSONResult list(@RequestBody @Valid ListRequestVo requestVo) {
		log.info("企业订购业务详情:" + JsonUtils.toJsonString(requestVo));
		return JSONResult.success(supplierCompanyService.list(requestVo));
	}

    /**
     * 提交-》新增供应商公司关联信息接口
     */
    @PostMapping("/add")
    @OperateLog(OperateLogContant.COMPANYADD)
    @ApiOperation("企业订购业务供应商设置新增接口")
    public JSONResult add(@RequestBody @Valid AddOrUpdateRequestVo requestVo) {
        log.info("新增企业订购业务信息:" + JsonUtils.toJsonString(requestVo));
        supplierCompanyService.add(requestVo);
        return JSONResult.ok();
    }

    /**
     * 提交-》修改供应商公司关联信息接口
     */
    @PostMapping("/update")
    @ApiOperation("企业订购业务供应商设置更新接口")
    @OperateLog(OperateLogContant.COMPANYUPDATE)
    public JSONResult update(@RequestBody @Valid AddOrUpdateRequestVo requestVo) {
        log.info("更新企业订购业务信息:" + JsonUtils.toJsonString(requestVo));
        supplierCompanyService.update(requestVo);
        return JSONResult.ok();
    }

    /**
     * 同步开卡功能
     * 将已经开卡的账号进行更新，未开卡的账号进行开卡
     */
    @PostMapping("/syncOpenCard")
    @ApiOperation("企业订购业务供应商同步开卡接口")
    public JSONResult<SyncOpenCardResponseVo> syncOpenCard(@RequestBody @Valid SyncOpenCardRequestVo request,BaseUserInfo baseUserInfo) {
        log.info("企业同步开卡:{}" , JsonUtils.toJsonString(request));
        SyncOpenCardResponseVo response = openCardService.applyOpenCard(request.getOrgId(),baseUserInfo.getAccountName());
        return JSONResult.success(response);
    }
    /**
     * 获取同步开卡结果功能
     */
    @PostMapping("/getSyncOpenCardResult")
    @ApiOperation("企业订购业务供应商同步开卡接口结果")
    public JSONResult<OpenCardListInfoVo> getSyncOpenCardResult(@RequestBody @Valid GetSyncOpenCardResultRequestVo request) {
        log.info("企业同步开卡结果:{}" , JsonUtils.toJsonString(request));
        OpenCardListInfoVo response = openCardService.getOpenCardRecord(request.getOrgId(), request.getPageNum(), request.getPageSize(),request.getSubAccountCode(),request.getSupplierAccountId());
        return JSONResult.success(response);
    }

    /**
     * 获取同步开卡详细结果功能
     */
    @PostMapping("/getSyncOpenCardResultDetail")
    @ApiOperation("企业订购业务供应商同步开卡接口结果详情")
    public JSONResult<OpenCardDetailListInfoVo> getSyncOpenCardResultDetail(@RequestBody @Valid GetSyncOpenCardResultDetailRequestVo request, BaseUserInfo baseUserInfo) {
        log.info("企业同步开卡结果:{}" , JsonUtils.toJsonString(request));
        OpenCardDetailListInfoVo response = openCardService.getOpenCardRecordDetail(request.getOrgId(), request.getBatchNo(),request.getPageNum(), request.getPageSize(),request.getFailedType());
        return JSONResult.success(response);
    }



    /**
     * 删除供应商公司关联信息
     */
    @RequestMapping("/delete")
    @OperateLog(OperateLogContant.COMPANYDEL)
    public JSONResult delete(@RequestBody @Valid DeleteRequestVo requestVo) {
        log.info("删除企业订购业务信息:" + JsonUtils.toJsonString(requestVo));
        supplierCompanyService.delete(requestVo);
        return JSONResult.ok();
    }

    /**
     * 新增修改供应商公司关联信息初始化接口
     */
    @RequestMapping("/init")
    @ApiOperation("初始化企业订购业务接口")
    public JSONResult<InitResponseVo> init(@RequestBody @Valid InitRequestVo requestVo) {
        log.info("初始化企业订购业务信息:" + JsonUtils.toJsonString(requestVo));
        return JSONResult.success(supplierCompanyService.init(requestVo));
    }

    /**
     * 供应商排序
     */
    @RequestMapping("/sort")
    public JSONResult sort(@RequestBody @Valid SortRequestVo requestVo) {
        log.info("排序企业订购业务信息:" + JsonUtils.toJsonString(requestVo));
        supplierCompanyService.sort(requestVo);
        return JSONResult.ok();
    }

    /**
     * 启用停用开关接口
     */
    @RequestMapping("/enable")
//    @OperateLog(OperateLogContant.COMPANYENABLE)
    public JSONResult enable(@RequestBody @Valid EnableRequestVo requestVo) {
        log.info("启用停用企业订购业务信息:" + JsonUtils.toJsonString(requestVo));
        supplierCompanyService.enable(requestVo);
        return JSONResult.ok();
    }

    /**
     * 获取供应商产品信息 soa接口
     */
    @RequestMapping("/findSupplierProduct")
    public JSONResult<SupplierProductBo> findSupplierProduct(@RequestBody @Valid GetSupplierProductRequestBo requestBo) {
        log.info("获取供应商产品信息:" + JsonUtils.toJsonString(requestBo));
        return JSONResult.success(supplierCompanyService.findSupplierProduct(requestBo));
    }

    /**
     * 获取供应商产品信息 soa接口
     */
    @RequestMapping("/listSupplierProduct")
    public JSONResult<List<SupplierProductBo>> listSupplierProduct(@RequestBody @Valid ListSupplierProductRequestBo requestBo) {
        log.info("获取供应商产品信息:" + JsonUtils.toJsonString(requestBo));
        return JSONResult.success(supplierCompanyService.listSupplierProduct(requestBo));
    }

    /**
     * 获取支持的供应商信息
     */
    @RequestMapping("/listSupplierInfo")
    public JSONResult<List<SupportSupplierInfoVo>> listSupplierInfo(BaseUserInfo userInfo) {
        log.info("获取支持的供应商信息:" + JsonUtils.toJsonString(userInfo));
        return JSONResult.success(supplierCompanyService.listSupplierInfo(userInfo.getCorpId()));
    }

    /**
     * 获取企业订购业务信息 soa接口
     */
    @RequestMapping("/findSupplierCompany")
    public JSONResult<SupplierCompanyBo> findSupplierCompany(@RequestBody @Valid GetSupplierCompanyRequestBo requestBo) {
        log.info("获取企业订购业务信息:" + JsonUtils.toJsonString(requestBo));
        return JSONResult.success(supplierCompanyService.findSupplierCompany(requestBo));
    }

    @PostMapping("/loadSupplierInfo")
    public JSONResult<List<MbSupplierInfoVo>> loadSupplierInfo(@RequestBody @Valid LoadSupplierInfoReq req) {
        List<MbSupplierInfo> mbSupplierInfo = supplierCompanyService.loadSupplierInfo4Company(req.getSupplierCodes(), req.getCompanyCode());
        if (CollectionUtils.isEmpty(mbSupplierInfo)) {
            return JSONResult.success(null);
        }
        return JSONResult.success(mbSupplierInfo.stream().map(msi -> {
            MbSupplierInfoVo msiv = new MbSupplierInfoVo();
            BeanUtils.copyProperties(msi, msiv);
            return msiv;
        }).collect(Collectors.toList()));
    }

    /**
     * 获取企业订购业务信息 soa接口
     */
    @RequestMapping("/listSupplierCompany")
    public JSONResult<List<SupplierCompanyBo>> listSupplierCompany(@RequestBody @Valid ListSupplierCompanyRequestBo requestBo) {
        log.info("获取企业订购业务信息:" + JsonUtils.toJsonString(requestBo));
        return JSONResult.success(supplierCompanyService.listSupplierCompany(requestBo));
    }

    /**
     * 获取公司订购服务商的公私类型 soa接口
     */
    @RequestMapping("/listPubOwnType")
    public JSONResult<List<String>> listPubOwnType(@RequestBody @Valid ListPubOwnRequestBo requestBo) {
        log.info("获取公司订购服务商的公私类型:" + JsonUtils.toJsonString(requestBo));
        return JSONResult.success(supplierCompanyService.listPubOwnType(requestBo));
    }

    /**
     * 根据供应商code查询关联的公司
     */
    @RequestMapping("/listBySupplierCode")
    public JSONResult<List<String>> listBySupplierCode(@RequestParam String supplierCode) {
        log.info("根据供应商code查询关联的公司:" + JsonUtils.toJsonString(supplierCode));
        return JSONResult.success(supplierCompanyService.listBySupplierCode(supplierCode));
    }

    /**
     * 根据企业orgId 批量查询
     */
    @RequestMapping("/listByOrgIds")
    public JSONResult<List<SupplierCompanyBo>> listByOrgIds(@RequestParam List<String> orgIds) {
        log.info("企业订购业务详情:" + JsonUtils.toJsonString(orgIds));
        return JSONResult.success(supplierCompanyService.listByOrgIds(orgIds));
    }

    /**
     * 查询公司订阅某个供应商的产品信息
     */
    @ApiOperation("查询公司订阅供应商的产品信息")
    @PostMapping("/queryCompanySupplierProduct")
    public JSONResult<QueryCompanySupplierProductResponse> queryCompanySupplierProduct(
        @RequestBody @Valid QueryCompanySupplierProductRequest request) {
        return JSONResult.success(supplierCompanyService.queryCompanySupplierProduct(request));
    }

    @RequestMapping("/findCorpIdBySupplierCode")
    public JSONResult<SupplierProductBo> findCorpIdBySupplierCode(@RequestBody GetSupplierCompanyRequestBo requestBo) {
        log.info("查询供应商corpId参数:" + JsonUtils.toJsonString(requestBo));
        return JSONResult.success(supplierCompanyService.findCorpIdBySupplierCode(requestBo));
    }

    @RequestMapping("/getSupplierCompanySoa")
    public JSONResult<List<SupplierCompanyDto>> getSupplierCompanySoa(){
        return JSONResult.success(supplierCompanyService.getSupplierCompanySoa());
    }

    @PostMapping("/listSupplierUrl")
    public JSONResult<ListSupplierUrlRespVo> listSupplierUrl(@RequestBody ListSupplierUrlReqVo requestParam) {
        return new JSONResult<>(supplierCompanyService.listSupplierUrl(requestParam));
    }

}
